package com.lumlord.common.spring;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lumlord.common.shiro.config.JwtAuthFilter;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import com.lumlord.common.annotation.Api;
import com.lumlord.common.domain.Result;

/**
 * 参数返回
 * 
 * @author GPF 2019年9月9日 09:22:44
 */

public class ResponseReturnJsonBody extends RequestResponseBodyMethodProcessor
		implements HandlerMethodReturnValueHandler {
	
	 
 
	public ResponseReturnJsonBody(List<HttpMessageConverter<?>> converters) {
		super(converters);
	}

	@Override
	public boolean supportsReturnType(MethodParameter returnType) {
		Api api = returnType.getExecutable().getAnnotation(Api.class);
		if(null!=api) {
			return api.isJsonResponse() == true;
		}
		return false;
	}
	 
	@SuppressWarnings("unchecked")
	@Override
	public void handleReturnValue(Object o, MethodParameter methodParameter,
			ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest)
			throws IOException, HttpMediaTypeNotAcceptableException {
		Map ret = Result.success(o);
		HttpServletRequest servletRequest = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
		HttpServletResponse servletResponse = nativeWebRequest.getNativeResponse(HttpServletResponse.class);
		try{
			jwtAuthFilter.fillCorsHeader(servletRequest,servletResponse);
		}catch (Exception e){

		}
		 ret.put("executeTime", servletRequest.getAttribute("executeTime"));
		super.handleReturnValue(ret, methodParameter, modelAndViewContainer, nativeWebRequest);

	}

	protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest) {
		HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
		Assert.state(response != null, "No HttpServletResponse");
		return new ServletServerHttpResponse(response);
	}

	protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest) {
		HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
		Assert.state(servletRequest != null, "No HttpServletRequest");
		return new ServletServerHttpRequest(servletRequest);
	}

	private JwtAuthFilter jwtAuthFilter;
}
